﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using DWF.Common.Activities.Contract;
using DWF.Studio.Share.Serialization;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace DWF.Studio.ScriptEngine.Custom
{
    /// <summary>
    /// 工作流运行时服务
    /// </summary>
    internal class WorkflowRuntimeService : IRuntimeService
    {
        private string _projectDirectory = null;

        private string _scriptPath = null;

        private string _projectId = null;

        private string _assetsUri = null;

        public WorkflowRuntimeService(string projectId, string projectDirectory, string scriptPath, string assetsUri)
        {
            _projectDirectory = projectDirectory;
            _scriptPath = scriptPath;
            _projectId = projectId;
            _assetsUri = assetsUri;
        }

        public string GetAssets(string token, string assetsId)
        {
            var environment = 1;
            var hearder = "Robot 01";
            return getRemoteData(_assetsUri, hearder, assetsId, environment, token);
        }

        public string GetProjectDirectory()
        {
            return _projectDirectory;
        }

        public string GetProjectId()
        {
            return _projectId;
        }

        public string GetScriptPath()
        {
            return _scriptPath;
        }


        /// <summary>
        /// 请求远程服务端数据
        /// </summary>
        /// <param name="url">URL</param>
        /// <param name="hearder">请求头</param>
        /// <param name="assetsId">SID</param>
        /// <param name="environment">环境</param>
        /// <param name="token">TOKEN</param>
        private string getRemoteData(string url, string hearder, string assetsId, int environment, string token)
        {
            string res = string.Empty;
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "post";
                request.ContentType = "application/json";
                request.Headers.Add("Authorization", hearder);
                //生成payload
                Dictionary<string, object> payload = new Dictionary<string, object>();
                payload.Add("assetsId", assetsId);
                payload.Add("environment", environment);
                payload.Add("token", token);

                //序列化payload,转为json格式
                string payjson = JsonHelper.Serialize(payload);

                byte[] data = Encoding.UTF8.GetBytes(payjson);
                request.ContentLength = data.Length;
                request.GetRequestStream().Write(data, 0, data.Length);
                request.GetRequestStream().Close();

                //获取响应
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                //获取响应流
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    res = reader.ReadToEnd();
                }
            }
            catch
            {
                throw;
            }

            return res;
        }
    }
}
